using System.Threading;
using System.Threading.Tasks;
using MediatR;
using B.S.BasicData.EroorCode;
using B.S.BasicData.Domain;
using B.S.BasicData.Infrature;

public class BreedingImmunityInfoAddHandler : IRequestHandler<BreedingImmunityInfoAddRequest, ApiResult<BreedingImmunityInfo>>
{
    private readonly IBaseRepository<BreedingImmunityInfo> _repository;
    public BreedingImmunityInfoAddHandler(IBaseRepository<BreedingImmunityInfo> repository)
    {
        _repository = repository;
    }

    public async Task<ApiResult<BreedingImmunityInfo>> Handle(BreedingImmunityInfoAddRequest request, CancellationToken cancellationToken)
    {
        var entity = new BreedingImmunityInfo
        {
            FeedingDays = request.FeedingDays,
            ImmunityType = request.ImmunityType,
            ImmunityMethod = request.ImmunityMethod,
            BatchNo = request.BatchNo,
            IsDeleted = request.IsDeleted
        };
        try
        {
            await _repository.AddAsync(entity);
            return ApiResult<BreedingImmunityInfo>.Success(entity, "添加成功");
        }
        catch (System.Exception ex)
        {
            return ApiResult<BreedingImmunityInfo>.Fail($"添加失败: {ex.Message}");
        }
    }
} 